package com.suse.springmvcdemo2.controller;


import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/login")
    public Boolean login(String userName, String password,HttpSession session) {
        //账户或密码为空
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password))
            return false;

        //账户和密码正确
        if("zhangsan".equals(userName) && "123".equals(password)) {
            session.setAttribute("userName",userName);
            return true;
        }

        //除此之外为错误
        return false;
    }
    @RequestMapping("/getUserInfo")
    public String getUserInfo(HttpSession session) {
        String userName = (String)session.getAttribute("userName");
        System.out.println("登录用户为："+userName);
        return userName == null ? "" :userName;
    }
}
